/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.tree;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.tree.TreePath;

import org.jxSwing.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.tree
 * @date 14/06/2014 - 09:24:23
 */
public class FileTree extends Tree {
	
	private static final long serialVersionUID = -4627790206560550101L;
	
	/**
	 * 
	 */
	public FileTree() {
	
		this(false);
	}
	
	/**
	 * @param showHideFiles
	 */
	public FileTree(final boolean showHideFiles) {
	
		this(null, showHideFiles, false);
	}
	
	/**
	 * @param showHideFiles
	 * @param showOnlyDirectories
	 */
	public FileTree(final boolean showHideFiles, final boolean showOnlyDirectories) {
	
		this(null, showHideFiles, showOnlyDirectories);
	}
	
	/**
	 * @param root
	 * @param showHideFiles
	 * @param showOnlyDirectories
	 */
	public FileTree(final File root, final boolean showHideFiles, final boolean showOnlyDirectories) {
	
		super(new FileTreeModel(root, showHideFiles, showOnlyDirectories));
	}
	
	/**
	 * @param file
	 * @return {@link TreePath}
	 */
	public TreePath[] filesToTreePaths(final List<File> files) {
	
		final TreePath[] treePaths = new TreePath[files.size()];
		
		int index = 0;
		
		//
		for(final File file : files) {
			
			treePaths[index++] = fileToTreePath(file);
		}
		
		return treePaths;
	}
	
	/**
	 * @param file
	 * @return {@link TreePath}
	 */
	public TreePath fileToTreePath(final File file) {
	
		return new TreePath(file);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JTree#getModel()
	 */
	@Override
	public FileTreeModel getModel() {
	
		return (FileTreeModel)super.getModel();
	}
	
	/**
	 * @return the root
	 */
	public File getRoot() {
	
		return getModel().getRoot();
	}
	
	/**
	 * @return the selectedFile or null
	 */
	public File getSelectedFile() {
	
		return treePathToFile(getSelectionPath());
	}
	
	/**
	 * @return the selectedFiles
	 */
	public List<File> getSelectedFiles() {
	
		final List<File> selectedFiles = new ArrayList<>();
		
		//
		if(getSelectionPaths() != null) {
			
			//
			for(final TreePath treePath : getSelectionPaths()) {
				
				selectedFiles.add(treePathToFile(treePath));
			}
		}
		
		return selectedFiles;
	}
	
	/**
	 * 
	 */
	public final void refresh() {
	
		setModel(new FileTreeModel(getRoot(), showHideFiles(), showOnlyDirectories()));
	}
	
	/**
	 * @return the showHideFiles
	 */
	public final boolean showHideFiles() {
	
		return getModel().showHideFiles();
	}
	
	/**
	 * @return the showOnlyDirectories
	 */
	public final boolean showOnlyDirectories() {
	
		return getModel().showOnlyDirectories();
	}
	
	/**
	 * @param treePath
	 * @return {@link File}
	 */
	public File treePathToFile(final TreePath treePath) {
	
		String path = treePath.toString();
		
		path = path.replace(", ", File.separator);
		path = path.replace("[", "");
		path = path.replace("]", "");
		path = path.replace(I18N.system().computer().concat(File.separator), "");
		
		//
		if(path.startsWith(File.separator + File.separator)) {
			
			path = path.substring(1);
		}
		
		return new File(path);
	}
}
